<?php

namespace App\Http\Controllers\Home\Goods\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Goods;
use App\Model\Gprice;
use App\Model\Gshow;
use App\Model\Gdescr;
use Illuminate\Support\Facades\Redis; 
use App\Model\Sale_promotion;
use App\Model\Sort;
use App\Http\Controllers\Home\Index\Api\IndexApi;

class GoodsApi extends Controller
{
    /**
     * 商品的详细信息的查询
     *  return $onedata
     */
     public function details($gid)
     {
     	// $gid =11;
    	// $pid =1;
        if (!Redis::hexists("hotData","hotData:$gid")) {

              //查询goods表
            $good = Goods::select(['id', 'goods_sn', 'gname', 'price', 'unit', 'sales', 'click', 'pic', 'is_member_price', 'score', 'state'])
                ->where('id', $gid)
                ->first()
                ->toArray();

            $hotInfo['goods'] = $good;

            //查询goods_price表
            $good_price = Gprice::select(['id', 'price', 'discount_price', 'storage', 'size', 'color', 'sales', 'is_discount', 'state'])
                ->where('gid', $gid)
                ->get()
                ->toArray();

            $hotInfo['goods_price'] = $good_price;
            
             //处理goods_price数据,以用于商品详情页遍历颜色,尺寸,价格之间的笛卡尔关系
            $relation = new \stdClass();
            foreach ($good_price as $v) {

                $key = $v['color'] . '_' . $v['size'];
                $key = urldecode($key);
                $relation->$key['price'] = $v['discount_price'];
                $relation->$key['mktprice'] = $v['price'];
                $relation->$key['storage'] = $v['storage'];
                 $relation->$key['id'] = $v['id'];
            }
            
            $hotInfo['relation'] = urldecode(json_encode($relation));

            //查询goods_show表
            $good_pic = Gshow::select(['id', 'path', 'state', 'descr'])
                ->where('gid', $gid)
                ->get()
                ->toArray();

            $hotInfo['goods_pic'] = $good_pic;

            //查询goods_descr表
            $good_descr =  Gdescr::select(['descr'])
                ->where('gid', $gid)
                ->where('state', 1)
                ->value('descr');
                
            $hotInfo['descr'] = $good_descr;

            // dd($hotInfo);
            
            //以哈希类型,存入Redis. key为hotData, field为hotData:$gid
            Redis::hSet('hotData', 'hotData:'.$gid, json_encode($hotInfo));
            Redis::expire('hotData', 60*60*24);  //有效期为1天

            $onedata = json_decode(Redis::hget("hotData","hotData:$gid"));

            }else{

                 $onedata = json_decode(Redis::hget("hotData","hotData:$gid"));

            }
            return $onedata;
     }
     /**
      * 详情页中间的特惠促销模块
      * return $hotSale
      */
     public function loadHotSale()
        {   
           if (Redis::get('homeHotSale')) {

                return json_decode(Redis::get('homeHotSale'), true);

            }else {

         
            $nowTime = time();

            $hotSale = Sale_promotion::select(['id', 'gid', 'pid', 'gname', 'sale_price', 'path'])
                    ->where('sale_end_time', '>', $nowTime)
                    ->where('ad_address', 1)
                    ->limit(6)
                    ->get()
                    ->toArray();

            Redis::set('homeHotSale', json_encode($hotSale));
            Redis::expire('homeHotSale', 60*60*2);     //每2个小时刷新一次
        }
     
            return $hotSale;
        }
    /**
     * 详情页左下角的分类模块
     * return $goodsort
     */
    public function goodSort()
    {
        //先从Redis查找分类信息,如果有则return回去
        if (Redis::get('detailSort')){
            return json_decode(Redis::get('detailSort'), true);  //把json字符串转化成数组return出去
        }

        $goodsort = Sort::select(['id', 'pid', 'sname', 'state'])
                    ->where('pid', 0)
                    ->where('state', 1)
                    ->get()
                    ->toArray();
        foreach($goodsort as &$sonsort){
                $pid_id = $sonsort['id'];
                $secondSortObj = Sort::select(['id', 'pid', 'sname', 'state'])
                                ->where('pid', $pid_id)
                                ->where('state', 1)
                                ->get()
                                ->toArray();

                foreach ($secondSortObj as &$childsort){
                                $son_id =$childsort['id'];
                                $thirdSortObj = Sort::select(['id', 'pid', 'sname', 'state'])
                                        ->where('pid', $son_id)
                                        ->where('state', 1)
                                        ->get()
                                        ->toArray();
                                if(!empty($thirdSortObj)){
                                        $childsort['thirdSort'] = $thirdSortObj;  //放置三级分类
                                          }
                                    }
                if(!empty($sonsort)){

                              $sonsort['secondort'] = $secondSortObj;  //放置二级分类

                         }
                Redis::set('detailSort', json_encode($goodsort));   //把数据存入Redis
                Redis::expire('detailSort', 60*60*24*7);    //设置Redis的key时间为7天
               }
        // dd($goodsort);
        return $goodsort;
    }

    /**
     * 详情页左下角的店铺热销模块
     * return $hotSale
     */
    public function shopHotSale()
        {   
            if (Redis::get('hotSale')) {

            return json_decode(Redis::get('hotSale'), true);
             }
            $nowTime = time();

            $hotSale = Sale_promotion::select(['id', 'gid', 'pid', 'gname', 'sale_price', 'path'])
                    ->where('sale_end_time', '>', $nowTime)
                    ->limit(3)
                    ->where('ad_address', 1)
                    ->get()
                    ->toArray();
            Redis::set('hotSale', json_encode($hotSale));
            Redis::expire('hotSale', 60*60*4);  //4小时刷新一次
            return $hotSale;
        }

    /**
     * 详情页商品右边的热销商品
     * return $detailHotSale
     */
    public function detailShopHotSale()
        {   
            if (Redis::get('detailHotSale')) {

            return json_decode(Redis::get('detailHotSale'), true);
             }
            $nowTime = time();

            $detailHotSale = Sale_promotion::select(['id', 'gid', 'pid', 'path'])
                    ->where('sale_end_time', '>', $nowTime)
                    ->where('ad_address', 4)
                    ->limit(2)
                    ->get()
                    ->toArray();
            Redis::set('detailHotSale', json_encode($detailHotSale));
            Redis::expire('detailHotSale', 60*60*2);   //每2个小时刷新一次
            return $detailHotSale;
        }
    
}
